Ubuntu 下通过 certbot 安装 SSL 证书以及证书的自动更新.

momo314相同方式共享非商业用途署名转载

使用 LetsEncrypt 提供的 SSL 证书,并且使用其推荐工具 certbot 工具进行证书的 安装自动更新

1. 添加 Certbot PPA

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update

2. 安装 Certbot

sudo apt-get install certbot python-certbot-nginx

3. 安装 SSL 证书,并更新 nginx 配置文件

sudo certbot --nginx

注意: 第一次使用 certbot 安装 SSL 证书时,与其他方式相同,会要求验证域名是否为本人持有,因此会请求以下Url:

http://your.domain.com/.well-known/acme-challenge/some-key

所以,安装 SSL 证书之前,需要保证此Url是可以正常访问的。为了这个验证去修改代码太不划算了,那么,最简单的办法就是在 nginx 的配置文件中加入如下配置:


server {
    listen 80;
    charset utf-8;
    server_name your.domain.com;
    location / {
        proxy_pass ...;
        proxy_http_version ...;
        proxy_set_header ...
        proxy_set_header ...
        proxy_set_header ...
    }

    # add this section for certbot domain verify
    # and remove this section after certbot running
    location ^~ /.well-known/acme-challenge/ {
        default_type "text/plain";
        root /var/www/letsencrypt; # ensure this folder exists
    }
}

修改完 nginx 配置文件后记得执行 sudo nginx -t 并在验证过后执行 sudo nginx -s reload,以确保配置生效。

再次执行 sudo certbot --nginx 命令之后,不出意外的话,应该就会提示 SSL 证书安装成功,此时查看 nginx 配置文件,就会发现 certbot 自动添加了 SSL证书 相关配置。

4. SSL 证书的自动更新

由于 LetsEncrypt 的免费 SSL证书 一般有效期为90天,如果每3个月都要手动来更新一次的话,实在是太麻烦了,所以 certbot 也提供了自动更新机制:

sudo certbot renew --dry-run

如果没有异常信息的话,那么 certbot 则会以每天两次的频率检查 SSL 证书是否过期,如果即将过期,就会自动更新证书。

✎﹏ 本文来自于 momo314和他们家的猫,文章原创,转载请注明作者并保留原文链接。